$(document).ready(function() {
	
	//回到顶部按钮对象
	var topcontrol  = $('#topcontrol');
	
	//导航条对象
	var nav = $('.nav-wrap');
	
	//导航条的实际高度
	var navHeight = nav.outerHeight();
	
	//导航条的相对网页原点的位置
	var navPos = nav.offset().top;
	
	//滚动条事件
	$(window).scroll(function() {
		
		//获得当前滚动条位置
		var sTop = $(window).scrollTop();
		
		//动态显示隐藏回到顶部的按钮
		if (sTop >= 200) {
			topcontrol.fadeIn(500);
		} else {
			topcontrol.fadeOut(500);
		}
		
		//自动给导航条设置fixed样式
		if (sTop >= navPos) {
			if (!nav.hasClass('fix')){
				nav.addClass('fix');
				//用margin占位
				$('.banner').css('margin-bottom',navHeight);
			}
		} else {
			if(nav.hasClass('fix')) {
				nav.removeClass('fix');
				//取消margin占位
				$('.banner').css('margin-bottom',0);
			}
		}
		
		//高亮函数
		function active(obj) {
			$('.nav-wrap li').removeClass('active');
			
			if(!obj.hasClass('active')) {
				obj.addClass('active');
			}
		}
		
		
		//监听滚动条区域
		//海量正版免费下
		var introPos = {
			start:$('#post-intro').offset().top - navHeight,
			end:$('#post-intro').offset().top + $('#post-intro').outerHeight() - navHeight
		}
		
		if (sTop >= introPos.start && sTop < introPos.end) {
			active( $('.intro') );
		} else {
			$('.intro').removeClass('active')
		}
		
		//无需苹果账号
		var usagePos = {
			start:$('#post-usage').offset().top - navHeight,
			end:$('#post-usage').offset().top + $('#post-usage').outerHeight() - navHeight
		}
		
		if (sTop >= usagePos.start && sTop < usagePos.end) {
			active( $('.usage') );
		} else {
			$('.usage').removeClass('active')
		}
		
		//无需苹果账号
		var choicePos = {
			start:$('#choiceness').offset().top - navHeight,
			end:$('#choiceness').offset().top + $('#choiceness').outerHeight() - navHeight
		}
		
		if (sTop >= choicePos.start && sTop < choicePos.end) {
			active( $('.choice') );
		} else {
			$('.choice').removeClass('active')
		}
	});
	
	//回到顶部功能
	topcontrol.click(function() {
		
		$('html,body').animate({scrollTop:0},1000);
	});
	
	//锚点链接过渡动画
	$('.nav-wrap a').click(function() {
		
		//获得对应板块相对于网页原点的位置
		var objTop = $(this.hash).offset().top - navHeight + 7;
		
		$('html,body').animate({scrollTop:objTop},1000);
		
		return false;
	})
});